//
//  DebugView.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/10/18.
//

import SwiftUI

struct DebugView: View {
  let openId = ViewModel.shared.passenger.openId
  let order = ViewModel.shared.order
  let passenger = ViewModel.shared.passenger
  let paddingSize :CGFloat = 6
  @Binding var showDebug : Bool
  @State var debugMode : Bool = ViewModel.shared.debugMode

  var body: some View {
    GeometryReader { geometry in
      VStack {
        HStack {
          Text("Debug Information")
            .font(.system(size: 18))
            .padding()
        }

        Divider()

        VStack(alignment: .center) {
          HStack {
            Text("用户编号:")
            Text("\(openId)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])
          HStack {
            Text("订单编号:")
            Text("\(order.orderId)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])
          HStack {
            Text("车牌号码:")
            Text("\(order.carNo)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])
          HStack {
            Text("司机编号:")
            Text("\(order.driverId)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])

          HStack {
            Text("司机微信:")
            Text("\(order.driverWechat)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])
          HStack {
            Text("司机手机:")
            Text("\(order.driverPhone)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])
        }

        Divider()

        VStack {
          HStack {
            Text("乘客姓名:")
            Text("\(passenger.name)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])

          HStack {
            Text("乘客性别:")
            Text("\(passenger.isMale.toString(isTrue: "男", isFalse: "女"))").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])

          HStack {
            Text("乘客手机:")
            Text("\(passenger.mobile)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])

          HStack {
            Text("乘客老挝手机:")
            Text("\(passenger.laosMobile)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])

          HStack {
            Text("乘客微信:")
            Text("\(passenger.wechatNo)").fontWeight(.bold)
            Spacer()
          }.padding(.top,paddingSize).padding([.trailing,.leading])
        }

        Button {
          showDebug.toggle()
        } label: {
          Text("关闭").padding().overlay(
            RoundedRectangle(cornerRadius: 8, style: .continuous)
              .stroke(Color.gray, lineWidth: 1))
        }.padding()

        Spacer()
        HStack {
          Toggle("调试模式", isOn: $debugMode).padding()
        }.padding()
      }
    }.background(.white)
      .cornerRadius(8)
      .foregroundColor(.black)
      .overlay(
        RoundedRectangle(cornerRadius: 8, style: .continuous)
          .stroke(Color.gray, lineWidth: 1).shadow(radius: 5))
      .onChange(of: debugMode, perform: { newValue in
        ViewModel.shared.debugMode = newValue
        UserDefaults.standard.setValue(newValue, forKey: "debugMode")
      })
  }
}

struct DebugView_Previews: PreviewProvider {
    static var previews: some View {
      DebugView(showDebug:.constant(true),debugMode:true)
    }
}
